package com.weiyuwqj.server.web;


import com.weiyuwqj.api.constant.Global;
import com.weiyuwqj.api.model.SysConfigParameter;
import com.weiyuwqj.api.service.SysConfigParameterService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.List;

/**
 * Created by huzj on 2016/12/22.
 */
public class ConstantsServletContextListener implements ServletContextListener {


    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext context = servletContextEvent.getServletContext();
        ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
        SysConfigParameterService sysConfigParameterService = (SysConfigParameterService) ctx.getBean("configParameterService");
        List<SysConfigParameter> list = sysConfigParameterService.getList();
        for (int i = 0; i < list.size(); i++) {
            Global.getConfig().put(list.get(i).getParamKey(), list.get(i).getParamValue());
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
    }
}
